package com.mall.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.SmsHomeAdvertiseApi;
import com.mall.exception.ConsumerException;
import com.mall.pageResult.PageResult;
import com.mall.pojo.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class HomeAdvertiseService {

    @DubboReference
    private SmsHomeAdvertiseApi homeAdvertiseApi;

    /**
     * 分页查询首页推广告
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param endTime
     * @param type
     * @return
     */
    public PageResult<SmsHomeAdvertise> list(Integer pageNum, Integer pageSize, String name, String endTime, Integer type) {
        //查询首页广告分页信息
        Page<SmsHomeAdvertise> page = homeAdvertiseApi.list(pageNum, pageSize, name, endTime, type);
        //组装返回结果数据
        PageResult<SmsHomeAdvertise> result = new PageResult<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(page.getRecords());
        result.setTotal(Convert.toInt(page.getTotal()));
        result.setTotalPage(Convert.toInt(page.getPages()));
        return result;
    }

    /**
     * 修改首页广告上下线状态
     *
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Integer id, Integer status) {
        //根据id查询首页广告信息
        SmsHomeAdvertise advertise = homeAdvertiseApi.findById(id);
        //设置状态值
        advertise.setStatus(status);
        //修改
        Integer count = homeAdvertiseApi.update(advertise);
        return count;
    }

    /**
     * 添加广告
     *
     * @param advertise
     * @return
     */
    public Integer create(SmsHomeAdvertise advertise) {
        //起止时间严谨性判断
        Date endTime = advertise.getEndTime();
        Date startTime = advertise.getStartTime();
        if (endTime.getTime() - startTime.getTime() <= 0) {
            throw new ConsumerException("起止时间设置错误");
        }
        //判断是否以添加过
        SmsHomeAdvertise homeAdvertise = homeAdvertiseApi.findByURL(advertise.getUrl());
        if (ObjectUtil.isNotNull(homeAdvertise)){
            throw new ConsumerException("广告已存在");
        }
        //给要添加的广告信息设置默认值
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        //添加
        Integer count = homeAdvertiseApi.create(advertise);
        return count;
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     */
    public SmsHomeAdvertise advertiseMsg(Integer id) {
        return homeAdvertiseApi.findById(id);
    }

    /**
     * 修改广告
     * @param advertise
     * @return
     */
    public Integer update(SmsHomeAdvertise advertise) {
        //起止时间严谨性判断
        Date endTime = advertise.getEndTime();
        Date startTime = advertise.getStartTime();
        if (endTime.getTime() - startTime.getTime() <= 0) {
            throw new ConsumerException("起止时间设置错误");
        }
        Integer count = homeAdvertiseApi.update(advertise);
        return count;
    }

    /**
     * 批量删除广告
     * @param ids
     * @return
     */
    public Integer delete(Long[] ids) {
        Integer count = homeAdvertiseApi.deleteByIds(ids);
        return count;
    }
}
